﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Family.Core.Model;
using Family.Data.DBRepositories;
using Family.Data.Factories;
using Family.Core.DataInterfaces;
using FluentNHibernate.Data;

public partial class Statistics : System.Web.UI.Page
{
    private const String EMPTY_ENTITY = "[нет]";
    private const String EMPTY_VALUE = "0";

    private OrderRepository oRepo;
    private IRepository<Category> cRepo;
    private IRepository<Product> pRepo;

    private void BindDays(DateTime from, DateTime to)
    {
        days.DataSource = oRepo.GetOrdersByRange(from, to);
        days.DataBind();
    }

    private void BindCategories()
    {
        ddlCategory.Items.Clear();
        ddlCategory.Items.Add(new ListItem(EMPTY_ENTITY, EMPTY_VALUE));

        foreach (var item in cRepo.GetAll())
        {
            ddlCategory.Items.Add(new ListItem(item.Name, item.Id.ToString()));
        }
    }

    private void BindProducts()
    {
        ddlProduct.Items.Clear();
        ddlProduct.Items.Add(new ListItem(EMPTY_ENTITY, EMPTY_VALUE));

        foreach (var item in pRepo.GetAll())
        {
            ddlProduct.Items.Add(new ListItem(item.Name, item.Id.ToString()));
        }
    }

    private void BindProductsForCategory(Int32 categoryId)
    {
        ddlProduct.Items.Clear();
        ddlProduct.Items.Add(new ListItem(EMPTY_ENTITY, EMPTY_VALUE));

        var c = cRepo.Get(categoryId);

        foreach (var item in c.Products)
        {
            ddlProduct.Items.Add(new ListItem(item.Name, item.Id.ToString()));
        }
    }

    protected Statistics()
    {
        oRepo = new OrderRepository(WebFactory.CurrentSession);
        cRepo = new Repository<Category>(WebFactory.CurrentSession);
        pRepo = new Repository<Product>(WebFactory.CurrentSession);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCategories();
            BindProducts();
        }
    }

    protected void btnShow_Click(object sender, EventArgs e)
    {
        DateTime from, to;
        if (DateTime.TryParse(txtDateFrom.Text, out from) && DateTime.TryParse(txtDateTo.Text, out to))
        {
            BindDays(from, to);
        }
    }

    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        DateTime from, to;
        if (DateTime.TryParse(txtDateFrom.Text, out from) && DateTime.TryParse(txtDateTo.Text, out to))
        {
            Decimal result = oRepo.GetAmountByRange(Int32.Parse(ddlCategory.SelectedValue), Int32.Parse(ddlProduct.SelectedValue), from, to);
            lblAmount.Text = result.ToString("N2");
        }
    }

    protected void days_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            var uc = e.Item.FindControl("day") as Controls_DayStatisticControl;
            if (uc != null)
            {
                var order = e.Item.DataItem as Order;
                uc.BindOrder(order);
            }
        }
    }

    protected void ddlCategory_TextChanged(object sender, EventArgs e)
    {
        var id = Int32.Parse(ddlCategory.SelectedValue);
        if (id > 0)
        {
            BindProductsForCategory(id);
        }
    }
}